const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const multer = require('multer')
// let upload = multer({dest:'./files/images'})
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, './files/images')
    },
    filename: function (req, file, cb) {
      cb(null,  Date.now()+file.originalname)
    }
  })
  
let option = {
    storage:storage,
    fileFilter:function(req,file,cb){
       
            if(file.mimetype !== 'image/jpeg'){
                cb(null,false)
            }else{
                cb(null,true)
            }
        
        
    },
    limits:{
        fileSiz:1024*10,
        files:3
    }
}
// var upload = multer({ storage: storage })
var upload = multer(option)
const app = express()
// parse application/x-www-form-urlencoded
// app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.urlencoded())
// parse application/json
app.use(bodyParser.json())

app.use('/static',express.static(path.join(__dirname,'./public')))
app.get('/user/register',(req,res)=>{
    console.log(`请求参数是：${req.query}`);
    res.send(req.query)
})
app.post('/user/register',(req,res)=>{
    res.send(req.body)
})
app.post('/file/upload',upload.array('myfile',6),(req,res)=>{
    console.log(req.file);
    console.log(req.files);
    console.log(req.body);
    res.send('存储成功')
})
let server = app.listen(3000,()=>{
    let address = server.address().address
    let port = server.address().port
    console.log(`server is running hostname is ${address} port is ${port}`);

})